<template>
  <div class="page">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="服务器名称/IP" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label=""
        width="50"
        type="expand">
         <template slot-scope="props">
            <div style="width:100%;">
                <div v-if="props.row.datasources" v-for="(item,index) in props.row.datasources" style="width:32%;float:left;margin-right:1%;height: 80px;border:1px dashed #d4d4d4;display: flex;">
                  <icon-svg style="padding:10px;width:60px;height:80px;border-right:1px solid #d4d4d4;" :name="item.datasourceType"></icon-svg>
                   <div style="margin:3px 1px 0px 0px;width:740px;text-align:center;">
                       <div style="height:40px;text-align:center;">{{item.name}}</div>
                       <div style="height:30px;margin-right: 10px;text-align:right;">
                          <el-button type="text" size="middle" @click="toConfigFront(item)">配置</el-button>
                          <el-button type="text" size="middle" @click="toRestartBackend(item)">重启</el-button>
                       </div>
                   </div>
                 </div>
                 <div v-if="props.row.modules" v-for="(item,index) in props.row.modules" style="width:32%;float:left;margin-bottom:5px;margin-right:1%;height: 80px;border:1px dashed #d4d4d4;display: flex;">
                   <icon-svg style="padding:10px;width:60px;height:80px;border-right:1px solid #d4d4d4;" :name="item.moduleType=='gateway'||item.moduleType=='eureka'?'springboot':item.moduleType"></icon-svg>
                    <div style="margin:3px 1px 0px 0px;width:740px;text-align:center;">
                        <div style="height:40px;text-align:center;">{{item.name}}</div>
                        <div style="height:30px;margin-right: 10px;text-align:right;">
                           <el-button type="text" size="middle" @click="toConfigFront(item)">配置</el-button>
                           <el-button type="text" size="middle" @click="toRestartBackend(item)">重启</el-button>
                        </div>
                    </div>
                  </div>
            </div>
         </template>
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="left"
        align="left"
        label="服务器名称">
      </el-table-column>
      <el-table-column
        prop="ipAddr"
        header-align="center"
        align="center"
        label="IP地址">
      </el-table-column>
     <el-table-column
       prop="ipAddr"
       header-align="center"
       align="center"
        width="100"
       label="在线状态">
       <template slot-scope="scope">
       <el-tag v-if="scope.row.serverRuntime" size="small" type="danger">在线</el-tag>
       <el-tag v-else size="small">离线</el-tag>
       </template>
     </el-table-column>
      <el-table-column
        prop="cpu"
        header-align="center"
        align="center"
        width="100"
        label="CPU使用率">
        <template slot-scope="scope">
          {{scope.row.serverRuntime?parseFloat(scope.row.serverRuntime.cpu).toFixed(2):'--'}}%
        </template>
      </el-table-column>
      <el-table-column
        prop="mem"
        header-align="center"
        align="center"
        width="100"
        label="内存使用率">
        <template slot-scope="scope">
           {{scope.row.serverRuntime?(parseFloat(scope.row.serverRuntime.mem)*100).toFixed(2):'--'}}%
        </template>
      </el-table-column>
      <el-table-column
        prop="hard"
        header-align="center"
        align="center"
        width="100"
        label="硬盘使用率">
        <template slot-scope="scope">
          {{scope.row.serverRuntime?(parseFloat(scope.row.serverRuntime.hard)*100).toFixed(2):'--'}}%
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="100"
        label="当前连接数">
        <template slot-scope="scope">
          {{scope.row.serverRuntime?scope.row.serverRuntime.conn:'--'}}个
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        width="100"
        label="系统">
        <template slot-scope="scope">
          <icon-svg v-if="scope.row.type === 1"  name="centos"></icon-svg>
          <icon-svg v-if="scope.row.type === 2"  name="ubuntu"></icon-svg>
          <icon-svg v-if="scope.row.type === 3"  name="win"></icon-svg>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="100"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="SSH_Handle(scope.row)">远程</el-button>
          <el-button type="text" size="small" @click="SFTP_Handle(scope.row)">SFTP</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- SSH -->
    <ssh-handle v-if="sshHandleVisible" ref="sshHandle"></ssh-handle>

    <sftp-handle v-if="sftpHandleVisible" ref="sftpHandle"></sftp-handle>
    <config-nginx v-if="ConfigNginxVisible" ref="configNginx"></config-nginx>
  </div>
</template>

<script>
   import sshHandle from './ssh-handle'
   import sftpHandle from './sftp-handle'
   import configNginx from '../project/config-nginx'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        sshHandleVisible: false,
        sftpHandleVisible:false,
        ConfigNginxVisible:false,
      }
    },
    components: {
      sshHandle,
      sftpHandle,
      configNginx,
    },
    activated () {
      this.getDataList()
    },
    methods: {
      SSH_Handle(serverInfo){
        this.sshHandleVisible = true
        this.$nextTick(() => {
          this.$refs.sshHandle.init(serverInfo)
        })
      },
      SFTP_Handle(serverInfo){
        this.sftpHandleVisible = true
        this.$nextTick(() => {
          this.$refs.sftpHandle.init(serverInfo)
        })
      },
      toConfigFront(frontInfo){
        this.ConfigNginxVisible = true
        this.$nextTick(() => {
          this.$refs.configNginx.init(frontInfo)
        })
      },
      toRestartBackend(item){
        this.$confirm(`确定对${item.name}进行重启操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/publish/restartBackend'),
            method: 'post',
             params: this.$http.adornParams({'id':item.id})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/systemconfig/servers/treelist'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
    }
  }
</script>
